<?php
/**
 * Created by PhpStorm.
 * User: 张辉
 * Date: 2018/6/4
 * Time: 16:54
 */

/**
 * Class SmsService 短信发送操作类
 * @package app\common\library
 */
namespace app\common\service;
use app\common\library\CommonFun;
use app\common\model\pushlog\SmsLogModel;
use think\Request;

class SmsService
{
    /**
     * @var array Sms的实例
     */
    public static $instance = [];

    /**
     * @var object 操作句柄
     */
    public static $handler;

    /**
     * @var string  短信发送类配置
     */
    private static $sms_configure_cache_key = 'ihold:str:sms_configure';

    /**
     * @var array 短信发送类型
     */
    public static $sms_send_type = [
        0 => 'code',   // 验证码短信
        1 => 'notice',  // 通知类短信
    ];

    // 短信发送类型   0 验证码  1通知短信
    const SMS_SEND_TYPE_CODE = 0;
    const SMS_SEND_TYPE_NOTICE = 1;

    public static $sms_type;

    /**
     * 短信通道列表
     */
    const SMS_SENDER_LIST = [
        'YunPian' => [
            'value' => 'YunPian',
            'title' => '云片短信',
        ],
    ];

    /**
     * 自动初始化 Sms
     * @access public
     * @param  array $options 配置数组
     * @return \app\common\service\sms\Driver
     */
    public static function init( array $options = [] )
    {
        $redis = CommonFun::get_redis( 'default' );
        if (is_null(self::$handler)) {

            if ( empty($options) ) {
                $options = ['type'=>$redis->get( self::$sms_configure_cache_key )];
            }

            $type = self::$sms_type = !empty($options['type']) ? $options['type'] : self::SMS_SENDER_LIST['YunPian']['value'];

            $name = md5(serialize($options));

            if (true === $name || !isset(self::$instance[$name])) {
                $class = false === strpos($type, '\\') ?
                    '\\app\\common\\service\\sms\\driver\\' . ucwords( $type ) :
                    $type;
                if (true === $name) {
                    return new $class( $options );
                }

                self::$instance[$name] = new $class( $options );

            }

            self::$handler = self::$instance[$name];
        }

        return self::$handler;
    }

    /**
     * 发送验证码短信
     * @param array $param 短信配置
     * @return bool
     */
    public static function sendCode( $param )
    {
        $res = self::init()->sendCode( $param );

        $param['type'] = self::SMS_SEND_TYPE_CODE;
        $param['status'] = $res;

        // 记录短信发送
        self::createSms( $param );

        return $res;
    }


    /**
     * 记录短信
     * @param $param
     * @return bool
     */
    protected static function createSms( $param )
    {
        $time = time();
        $Request = Request::instance();
        $ip = $Request->ip();
        $ip = is_string( $ip ) ? $ip : json_encode( $ip );
        $sms_model = new SmsLogModel();
        $sms_model->add_data(
            [
                'mobile' => $param['mobile'],
                'sender' => self::$sms_type,
                'type'   => $param['type'],
                'status' => $param['status'],
                'content'   =>  $param['content'],
                'event'     => $param['event'],
                'ip' => $ip,
                'createtime' => $time
            ]
        );
        return true;
    }

    /**
     * 获取当前选中的短信通道
     */
    public static function getSelectSender()
    {
        $redis = CommonFun::get_redis( 'default' );
        return $redis->get( self::$sms_configure_cache_key ) ?: self::SMS_SENDER_LIST['YunPian']['value'];
    }

    /**
     * 设置短信通道
     * @param $smsSender
     * @return bool
     */
    public static function setSelectSender( $smsSender )
    {
        if ( !isset(self::SMS_SENDER_LIST[$smsSender]) ){
            return false;
        }

        $redis = CommonFun::get_redis( 'default' );
        return $redis->set( self::$sms_configure_cache_key, $smsSender );
    }

}